#!/bin/bash
#PATH=$PATH:'/cygdrive/c/mysql/bin/'
MYSQL_OPTIONS='--default-character-set=czech --skip-column-names -u root -pCYbV9hvDfcQNbquvgdHc moodle4vyuka'
if [ -z $1 ]; then
  echo "Pouziti: $0  <adresar moodle s ukoly>";
  echo "Vytvori soubory tokenuu pro vsechny soubory nalezene v podadresarich adresare";
  exit 1;
fi

BIN=`dirname $0`;

for dir_ukolu in $1/*; do
    id_ukolu=${dir_ukolu##*/}
    if [ ! -d $id_ukolu ] ; then
      mkdir $id_ukolu
    fi
    mysql $MYSQL_OPTIONS <<-KONEC
	select name from mdl_assignment where id=$id_ukolu;
	KONEC
    echo '******************************* id ukolu='$id_ukolu
  for dir_studenta in $dir_ukolu/*; do
    id_studenta=${dir_studenta##*/}
    printf 'id=%d ' $id_studenta
    mysql $MYSQL_OPTIONS  <<-KONEC2
    	select firstname,lastname from mdl_user where id=$id_studenta;
	KONEC2
    find $dir_studenta -iregex '.*\(pas\|dpr\)$' -print | xargs cat > $id_ukolu/$id_studenta.pasc
    if [ -s $id_ukolu/$id_studenta.pasc ] ; then
      $BIN/tokenize_PASCAL $id_ukolu/$id_studenta.pasc _main
    fi
    rm $id_ukolu/$id_studenta.pasc
  done
done
